package scantask.client.threeDObjects;

import gov.nasa.worldwind.layers.AirspaceLayer;

import java.util.Iterator;
import java.util.Vector;

import scantask.client.threeDObjects.exceptions.LeafPolygoneException;
import scantask.containers.Coordinate;

public class CompositePolygone implements ComponentPolygone{
	
	private Vector<ComponentPolygone> _children;

	public CompositePolygone() {
		this._children = new Vector<ComponentPolygone>();
	}
	
	
	@Override
	public ComponentPolygone clone() {
		ComponentPolygone ans = new CompositePolygone();
		for (int i = 0; i < this._children.size(); i++) {
			try {
				ans.add(this._children.get(i).clone());
			} catch (LeafPolygoneException e) {
				// AdirD - impossible exception
			}
		}
		return ans;
	}

	@Override
	public void draw() {
		for(int i = 0; i<this._children.size(); i++){
			this._children.get(i).draw();
		}
	}


	@Override
	public void add(ComponentPolygone polygone) {
		this._children.add(polygone);
	}


	@Override
	public boolean isLeaf() {
		return false;
	}


	@Override
	public void setLocation(Coordinate coordinate) {
		for (int i = 0; i < this._children.size(); i++) {
			this._children.get(i).setLocation(coordinate);
		}
	}
	
	@Override
	public void setDescription(String desc){
		Iterator<ComponentPolygone> iter = this._children.iterator();
		while (iter.hasNext()){
			iter.next().setDescription(desc);
		}
	}
	
	@Override
	public String getDescription(){
		if ((this._children == null) || (this._children.size() == 0)) {
			return "";
		}
		return this._children.elementAt(0).getDescription();
	}


	@Override
	public void init3DObject(AirspaceLayer layer){
		Iterator<ComponentPolygone> iter = this._children.iterator();
		while(iter.hasNext()){
			iter.next().init3DObject(layer);
		}
	}


	@Override
	public void moveUp(double amount) {
		for (int i = 0; i < this._children.size(); i++) {
			this._children.get(i).moveUp(amount);
		}
	}

}
